package com.yanqu.road.dao.impl.activity.memorypieces;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.memorypieces.MemoryPiecesProgress;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemoryPiecesDaoImpl extends TempDataObjectDao<MemoryPiecesProgress> {

    public static String getMemoryPiecesProgressSql = "select * from `t_u_activity_memory_pieces_progress` where `activityId`=? and `userId`=?";

    public MemoryPiecesProgress getMemoryPiecesProgress(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        MemoryPiecesProgress progress = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMemoryPiecesProgressSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                progress = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return progress;
    }


    public static String insertMemoryPiecesDataSql = "insert into t_u_activity_memory_pieces_progress(userId,activityId,piecesId,lightPieces) values (?,?,?,?)";

    @Override
    protected boolean addData(MemoryPiecesProgress data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPiecesId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getLightPieces())));
        if (getDb().executeNoneQuery(insertMemoryPiecesDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateMemoryPiecesDataSql = "update `t_u_activity_memory_pieces_progress` set piecesId=?,lightPieces=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(MemoryPiecesProgress data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getPiecesId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getLightPieces())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateMemoryPiecesDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected MemoryPiecesProgress parseData(ResultSet rs) throws SQLException {
        MemoryPiecesProgress data = new MemoryPiecesProgress();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setPiecesId(rs.getInt("piecesId"));
        data.setLightPieces(JSONObject.parseObject(rs.getString("lightPieces"), new TypeReference<List<Integer>>() {
        }));
        return data;
    }
}
